
const { exec } = require('../db/index')

class UserService {
  async add(ctx) {
    const { username, address, age, gender, birthday } = ctx.request.body;
    let sql = `insert into info (username, address, age, gender) values ('${username}', '${address}', '${age}', '${gender}')`
    let data = await exec(sql);
    return { data }
  }
  async delete(ctx) {
    const { id } = ctx.request.body.id;
    let sql = `delete from info where id = ${id}`;
    let data = await exec(sql);
    return { data }
  }
  async batchDelete(ctx) {
    // 1,2,3,4,5
    console.log(ctx.request.body.id)
    let sql = `delete from info where id in (${ctx.request.body.id})`
    console.log(sql, 'sql')
    let data = await exec(sql);
    return { data }
  }
  async page(ctx) {
    const { page = 0, size = 5 } = ctx.query;
    let sql = `select * from info limit ${size} offset ${page * size}`;
    let data = await exec(sql);
    let total = await exec(`select count(id) as num from info`);
    return { data, total: total[0].num }
  }
  async update(ctx) {
    const { id, username, address, age, gender, birthday } = ctx.request.body;
    let sql = `update info set username = '${username}', address='${address}',age='${age}',gender='${gender}' where id = ${id} `
    let data = await exec(sql);
    return { data }
  }
  async search(ctx) {
    const { keyword } = ctx.query;
    let sql = `select * from info where username like '%${keyword}%'`;
    let data = await exec(sql);
    return { data }
  }
}

module.exports = new UserService